<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">用户注册</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" prefix-icon="tn-icon-xingmingyonghumingnicheng" placeholder="账户名">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" prefix-icon="tn-icon-mima" placeholder="密码" clearable v-model="ruleForm.password">
                    </el-input>
                </el-form-item>
                <el-form-item prop="spassword">
                    <el-input type="password" prefix-icon="tn-icon-mima" placeholder="再一次输入密码" clearable v-model="ruleForm.spassword">
                    </el-input>
                </el-form-item>
                <el-form-item prop="phonenumber">
                    <el-input prefix-icon="tn-icon-dianhua" placeholder="电话号码 " clearable v-model="ruleForm.phonenumber" @keyup.enter.native="submitForm('ruleForm')">
                    </el-input>
                </el-form-item>
                <el-form-item prop="phonecode">
                    <el-row :gutter="10">
                        <el-col :span="14">
                            <el-input prefix-icon="tn-icon-dianhua" placeholder="验证码 " clearable v-model="ruleForm.phonecode" @keyup.enter.native="submitForm('ruleForm')">
                            </el-input>
                        </el-col>
                        <el-col :span="6">
                            <el-button  type = 'primary' @click="getCode()" :disabled='smsTimeValue'>  {{smscode}} </el-button>
                        </el-col>

                    </el-row>
                </el-form-item>
                
                <div class="login-btn">
                    <el-button type="primary" icon="tn-icon-zhuce" @click="submitForm('ruleForm')">注册</el-button>
                </div>
                <p class="login-tips"> 注意 : 两次密码需要一致。</p>
                <div class="linkreturn">
                    <a href="/login">已有账号，直接登陆</a>
                </div>
                
            </el-form>
        </div>
    </div>
</template>

<script>
    var AV = require('leancloud-storage');
     

    export default {
        data: function(){
            return {
                ruleForm: {
                    username: '',
                    password: '',
                    spassword: '',
                    phonenumber:'',
                    phonecode:''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    spassword:[
                        { required:true, message:'请再次输入密码', trigger:'blur'}
                    ],
                    phonenumber:[
                        { required:true,message:'请输入电话号码',trigger:'blur'},
                        { message:'请输入一个有效的电话号码',trigger: 'blur,change'}
                    ]
                },
                smscode:'获取验证码',
                smsTime:10,
            }
        },
        computed:{
            smsTimeValue:function(){               
                if(this.ruleForm.phonenumber.length==11&&this.smsTime==10)return false;
                return true;
            }
        },
        methods: {
            submitForm(formName) {                
                // debugger;
                if(this.ruleForm.password !== this.ruleForm.spassword){
                   this.ruleForm.password = this.ruleForm.spassword = ''
                   this.$message.error('输入密码不一致，请重新输入')
                }
                var that = this;

                this.$refs[formName].validate((valid) => {
                        if(valid)
                        {                            
                            var user = new AV.User();
                            user.setUsername(that.ruleForm.username);
                            user.setPassword(that.ruleForm.password);
                            user.setMobilePhoneNumber(that.ruleForm.phonenumber)
                            user.signUp().then(function (loginedUser) {      
                                debugger;
                                return loginedUser;
                            }).then(function( loginedUser){
                                // 设置账户类型为后台账户
                                loginedUser.set('accountType','bg')
                                loginedUser.save().then(()=>{
                                    that.$message.info('注册成功')

                                    console.log(loginedUser)
                                    that.$router.push('/login')
                                })

                            }).catch(error=>{
                                // alert();
                                console.log(JSON.stringify(error))
                                that.$message.error(error.rawMessage)                            
                            });
                        }
                        
                            

                    // if (valid) {
                    //     localStorage.setItem('ms_username',this.ruleForm.username);
                    //     this.$router.push('/');
                    // } else {
                    //     console.log('error submit!!');
                    //     return false;
                    // }
                });
            },
            getCode(){
                if(!this.ruleForm.phonenumber){
                    return;
                }

                let that = this;
                

                // AV.Object
                // console.log('检查通过')
                AV.Cloud.requestSmsCode(this.ruleForm.phonenumber).then( (success) =>{
                    let timer = setInterval(()=>{
                    this.smscode = `${this.smsTime}秒后重试`
                    this.smsTime-=1;

                    if(this.smsTime < 0){
                        this.smscode = '获取验证码'
                        this.smsTime = 10
                        clearInterval(timer);
                    }
                        

                    },1000);
                    this.$message('验证码发送成功，请查收')
                },  (error) =>{
                    this.$message.warning('验证码发送失败:'+ error.rawMessage)
                });
            }
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
        /* background-image: url(../../assets/login-bg.jpg); */
        background: skyblue no-repeat;
        background-size: 100%;
    }
    .ms-title{
        width:100%;
        line-height: 50px;
        text-align: center;
        font-size:20px;
        /* color: #fff; */
        border-bottom: 1px solid #ddd;
    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:350px;
        margin:-190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255,255,255, 0.3);
        overflow: hidden;
    }
    .ms-content{
        padding: 30px 30px;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        /* height:36px; */
        margin-bottom: 10px;
    }
    .login-tips{
        font-size:12px;
        line-height:30px;
        color:#fff;
    }
    .linkreturn{
        font-size: 12px;
        text-align: right;
        list-style: none;        
    }
</style>